// 
// DescriptorProvider.cs
//  
// Author:
//       Tomáš Vondráček <tomas.vondracek@gmail.com>
// 
// Copyright (c) 2010 Tomas Vondracek
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Linq;
using System.Collections.Generic;
using Common.Gui.GuiWinFormsLib.ExtensionNodes;
using Common.Interfaces.Descriptors;
using Mono.Addins;

namespace Common.Gui.GuiWinFormsLib.Descriptors
{
	public class DescriptorProvider
	{
		Dictionary<string, IList<IFormDescriptor>> descriptors;
		
		static DescriptorProvider instance;
		
		internal static DescriptorProvider GetInstance()
		{
			if (instance == null)
				instance = new DescriptorProvider();
		
			return instance;
		}
		
		private DescriptorProvider()
		{
			this.descriptors = new Dictionary<string, IList<IFormDescriptor>>();
		}
		
		/// <summary>
		/// load descriptors from addins
		/// </summary>
		internal void LoadDescriptors()
		{
			var descriptors = AddinManager.GetExtensionNodes<FormDescriptorNode>("/Common/Gui/GuiWinFormsLib/Descriptors/FormDescriptor");
			
			foreach (var descriptorNode in descriptors)
			{
				var descriptor = descriptorNode.GetDescriptor();
				var targetForm = descriptorNode.TargetFormName;
				
				if (!this.descriptors.ContainsKey(targetForm))
					this.descriptors.Add(targetForm, new List<IFormDescriptor>());
				this.descriptors[targetForm].Add(descriptor);
			}
			
			Logging.Logger.AddImportant(string.Format("Loaded {0} descriptors", descriptors.Count));
		}
		
		/// <summary>
		/// Find and apply descriptor for the form
		/// </summary>
		/// <param name="formName">
		/// A <see cref="System.String"/>
		/// Type of form: MainForm, AboutForm, FloatingForm
		/// </param>
		/// <param name="form">
		/// A <see cref="System.Windows.Forms.Form"/>
		/// object of form to apply descriptor to
		/// </param>
		/// <exception cref="ArgumentException"></exception>
		/// <exception cref="InvalidOperationException"></exception>
		/// <exception cref="ArgumentException"></exception>
		internal void Apply(string formName, System.Windows.Forms.Form form)
		{
			if (string.IsNullOrEmpty(formName))
				throw new ArgumentException("target form must be spoecified!", "formName");
			if (form == null)
				throw new ArgumentNullException("form", "target form can't be null!");
			if (this.descriptors.Count == 0)
				throw new InvalidOperationException("There are no loaded descriptors");
			if (!this.descriptors.ContainsKey(formName))
				throw new ArgumentException("There is no descriptor for such form - " + formName);
				//Logging.Logger.AddWarning("There is no descriptor for such form - " + formName);
				
			
			var formDescriptors = this.descriptors[formName];
			
			foreach(var descriptor in formDescriptors)
			{
				ApplyInternal(form, descriptor);
			}
		}
		
		private void ApplyInternal (System.Windows.Forms.Form form, IFormDescriptor descriptor)
		{
			if (descriptor.BackgroundColor.HasValue)
				form.BackColor = descriptor.BackgroundColor.Value;
//			if (! string.IsNullOrEmpty(descriptor.Icon))
//					form.Icon = null;// TODO form.Icon = null
			if (descriptor.ShowInTaskbar.HasValue)
				form.ShowInTaskbar = descriptor.ShowInTaskbar.Value;
			if (descriptor.Size.HasValue)
				form.Size = descriptor.Size.Value;
			if (!string.IsNullOrEmpty(descriptor.Title))
			    form.Text = descriptor.Title;
		}
	}
}

